package com.example.teaching_server.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 资源类型DTO
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "资源类型信息")
public class ResourceTypeDTO {

    @NotBlank(message = "类型代码不能为空")
    @Size(max = 50, message = "类型代码长度不能超过50个字符")
    @Schema(description = "类型代码", example = "DOCUMENT", requiredMode = Schema.RequiredMode.REQUIRED)
    private String code;

    @NotBlank(message = "类型标签不能为空")
    @Size(max = 100, message = "类型标签长度不能超过100个字符")
    @Schema(description = "类型标签", example = "文档", requiredMode = Schema.RequiredMode.REQUIRED)
    private String label;

    @Size(max = 200, message = "图标路径长度不能超过200个字符")
    @Schema(description = "类型图标URL", example = "/icons/document.png")
    private String icon;

    @Schema(description = "类型描述", example = "各类文档资源")
    private String description;

    @Schema(description = "排序顺序", example = "1")
    @Builder.Default
    private Integer sortOrder = 0;

    @Schema(description = "是否启用", example = "true")
    @Builder.Default
    private Boolean enabled = true;

    @Schema(description = "关联的资源数量", example = "150")
    private Long resourceCount;

    /**
     * 获取完整的类型标识
     */
    @Schema(description = "完整类型标识", accessMode = Schema.AccessMode.READ_ONLY)
    public String getFullIdentifier() {
        return code + " - " + label;
    }

    /**
     * 验证类型代码格式（基础验证）
     */
    @Schema(description = "类型代码是否有效", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isValidCode() {
        return code != null && code.matches("^[A-Z_]{1,50}$");
    }

    /**
     * 获取图标URL或默认图标
     */
    @Schema(description = "图标URL（带默认值）", accessMode = Schema.AccessMode.READ_ONLY)
    public String getIconWithDefault() {
        if (icon != null && !icon.trim().isEmpty()) {
            return icon;
        }
        // 根据类型代码返回默认图标
        if (code == null) return "/icons/default.png";

        return switch (code.toUpperCase()) {
            case "DOCUMENT" -> "/icons/document.png";
            case "VIDEO" -> "/icons/video.png";
            case "AUDIO" -> "/icons/audio.png";
            case "IMAGE" -> "/icons/image.png";
            case "PRESENTATION" -> "/icons/presentation.png";
            case "SPREADSHEET" -> "/icons/spreadsheet.png";
            default -> "/icons/default.png";
        };
    }

    /**
     * 判断是否为系统预设类型
     */
    @Schema(description = "是否为系统预设类型", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isSystemType() {
        if (code == null) return false;

        String[] systemTypes = {"DOCUMENT", "VIDEO", "AUDIO", "IMAGE", "PRESENTATION", "SPREADSHEET"};
        for (String systemType : systemTypes) {
            if (systemType.equalsIgnoreCase(code)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获取类型颜色（用于前端展示）
     */
    @Schema(description = "类型颜色代码", accessMode = Schema.AccessMode.READ_ONLY)
    public String getColor() {
        if (code == null) return "#666666";

        return switch (code.toUpperCase()) {
            case "DOCUMENT" -> "#3498db";
            case "VIDEO" -> "#e74c3c";
            case "AUDIO" -> "#9b59b6";
            case "IMAGE" -> "#2ecc71";
            case "PRESENTATION" -> "#f39c12";
            case "SPREADSHEET" -> "#1abc9c";
            default -> "#666666";
        };
    }

    /**
     * 构建默认资源类型
     */
    public static ResourceTypeDTO createDefault(String code, String label, String icon) {
        return ResourceTypeDTO.builder()
                .code(code)
                .label(label)
                .icon(icon)
                .sortOrder(0)
                .enabled(true)
                .build();
    }

    /**
     * 验证DTO是否完整
     */
    @Schema(description = "DTO是否完整有效", accessMode = Schema.AccessMode.READ_ONLY)
    public boolean isValid() {
        return code != null && !code.trim().isEmpty() &&
                label != null && !label.trim().isEmpty();
    }
}
